package com.ruoyi.customerSample.controller;

import com.ruoyi.common.annotation.Log;
import com.ruoyi.common.core.controller.BaseController;
import com.ruoyi.common.core.domain.AjaxResult;
import com.ruoyi.common.core.page.TableDataInfo;
import com.ruoyi.common.enums.BusinessType;
import com.ruoyi.common.utils.poi.ExcelUtil;

import com.ruoyi.customerSample.domain.BackSampleBill;
import com.ruoyi.customerSample.domain.SampleDetailBill;
import com.ruoyi.customerSample.domain.SampleReceptionBill;
import com.ruoyi.customerSample.service.IBackSampleBillService;
import com.ruoyi.customerSample.service.ISampleDetailBillService;
import com.ruoyi.customerSample.service.ISampleReceptionBillService;
import io.swagger.annotations.*;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import java.util.List;

/**
 * 退样登记Controller
 * 
 * @author lgq
 * @date 2023-05-15
 */
@RestController
@RequestMapping("/backSample/bill")
@Api(value="退样登记",tags = "退样登记接口")
public class BackSampleBillController extends BaseController
{
    @Autowired
    private IBackSampleBillService backSampleBillService;

    @Autowired
    private ISampleReceptionBillService sampleReceptionBillService;

    @Autowired
    private ISampleDetailBillService sampleDetailBillService;

    /**
     * 查询退样登记列表
     */
    @GetMapping("/list")
    @ApiOperation(value="查询退样登记列表")
    @ApiImplicitParams({
            @ApiImplicitParam(paramType="query", name = "pageNum", value = "页数", required = true, dataType = "String"),
            @ApiImplicitParam(paramType="query", name = "pageSize", value = "每页记录数", required = true, dataType = "String"),
    })
    public TableDataInfo list(BackSampleBill backSampleBill)
    {
        startPage();
        List<BackSampleBill> list = backSampleBillService.selectBackSampleBillList(backSampleBill);
        return getDataTable(list);
    }

    /**
     * 导出退样登记列表
     */
    @Log(title = "退样登记", businessType = BusinessType.EXPORT)
    @GetMapping("/export")
    public AjaxResult export(BackSampleBill backSampleBill)
    {
        List<BackSampleBill> list = backSampleBillService.selectBackSampleBillList(backSampleBill);
        ExcelUtil<BackSampleBill> util = new ExcelUtil<BackSampleBill>(BackSampleBill.class);
        return util.exportExcel(list, "bill");
    }

    /**
     * 获取退样登记详细信息
     */
    @GetMapping(value = "/{id}")
    @ApiOperation(value="获取退样登记详细信息")
    @ApiImplicitParam(paramType="query", name = "id", value = "主键", required = true, dataType = "String")
    public AjaxResult getInfo(@PathVariable("id") String id)
    {
        return AjaxResult.success(backSampleBillService.selectBackSampleBillById(id));
    }

    /**
     * 根据项目编号获取需要退样的样品明细
     */
    @GetMapping("/getDetailByItemNum/{itemNum}/{state}")
    @ApiOperation(value="根据项目编号获取需要退样的样品明细")
    @ApiImplicitParams({
            @ApiImplicitParam(paramType="query", name = "itemNum", value = "项目编号", required = true, dataType = "String"),
            @ApiImplicitParam(paramType="query", name = "state", value = "状态", required = true, dataType = "String"),
    })
    public AjaxResult getDetailByItemNum(@PathVariable("itemNum") String itemNum, @PathVariable("state") String state) {
        //根据项目编号获取收样单
        SampleReceptionBill sampleReceptionBill = sampleReceptionBillService.selectSampleReceptionBillByItemNum(itemNum);
        if(null == sampleReceptionBill) {
            return AjaxResult.error("获取来样单错误！");
        }
        //根据样品id和是否退样状态获取详情数据
        SampleDetailBill sampleDetailBill = new SampleDetailBill();
        sampleDetailBill.setSid(sampleReceptionBill.getId());
        sampleDetailBill.setIsBack(state);
        List<SampleDetailBill> list = sampleDetailBillService.selectSampleDetailBillList(sampleDetailBill);
        AjaxResult ajaxResult = new AjaxResult();
        ajaxResult.put("receiveTime", sampleReceptionBill.getReceiveTime());
        ajaxResult.put("data", list);
        ajaxResult.put("code", 200);
        return ajaxResult;
    }

    /**
     * 新增退样登记
     */
    @Log(title = "退样登记", businessType = BusinessType.INSERT)
    @PostMapping
    @ApiOperation(value="新增退样登记")
    public AjaxResult add(@ApiParam(name="退样登记",value="传入json格式",required=true)@RequestBody BackSampleBill backSampleBill)
    {
        return toAjax(backSampleBillService.insertBackSampleBill(backSampleBill));
    }

    /**
     * 修改退样登记
     */
    @Log(title = "退样登记", businessType = BusinessType.UPDATE)
    @PutMapping
    @ApiOperation(value="修改退样登记")
    public AjaxResult edit(@ApiParam(name="退样登记",value="传入json格式",required=true)@RequestBody BackSampleBill backSampleBill)
    {
        return toAjax(backSampleBillService.updateBackSampleBill(backSampleBill));
    }

    /**
     * 删除退样登记
     */
    @Log(title = "退样登记", businessType = BusinessType.DELETE)
	@DeleteMapping("/{ids}")
    @ApiOperation(value="删除退样登记")
    public AjaxResult remove(@PathVariable String[] ids)
    {
        return toAjax(backSampleBillService.deleteBackSampleBillByIds(ids));
    }
}
